package live.yanxiaohui.mideng.consumer;

import com.rabbitmq.client.Channel;
import live.yanxiaohui.mideng.MsgUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @Description todo
 * @CSDN https://blog.csdn.net/yxh13521338301
 * @Author： yanxh<br>
 * @Date 2020-06-29 10:25<br>
 * @Version 1.0<br>
 */
@Component
public class MiDengConsumer {

    @RabbitListener(queues = "game")
    public void consumer(Message message, Channel channel) throws Exception {
        String msg = new String(message.getBody());
        String messageId = message.getMessageProperties().getMessageId();
        if (MsgUtils.ids.containsKey(messageId)) {
            System.out.println("消息幂等，不可重复消费");
            // 通知mq删除信息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            return;
        }

        System.out.println("消费者消费消息：" + msg);
        MsgUtils.ids.put(messageId, true);
        // 通知mq删除信息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}


